
from qturboframe.lib.webdriver.exceptions import SeleniumError
from qturboframe.lib.report.log import logger
from BladeFerm.hui_app.hui_lib.select import Select as HuiSelect
import time

class Select(HuiSelect):
    '''
    对下拉框进行封装
    对操作员中心有的菜单右侧的其他checkbox进行封装
    '''

    def set_value_for_loading_multiple(self, value, timeout=20):
        """
        下拉继续加载的多选下拉框选择（label为角色信息）

        :Args:
         - value: 选择项的文本集合
        """
        select_ul_class = r'h-select-dropdown-list'
        # 先全部清空
        self.clear_for_multiple()
        # 弹出下拉框
        self._element.find_element_by_attribute(
            'css selector', 'div.h-select-selection', timeout=self.timeout).secure_click()
        select_div = self._element.find_element_by_attribute(
            'css selector', 'div.h-select-dropdown', timeout=self.timeout)
        timeout1 = 0
        while timeout1 < timeout * 10:
            select_ul = select_div.exist('css selector', 'ul.' + select_ul_class, timeout=0.1)
            if select_ul:
                if select_ul.is_displayed():
                    break
            timeout1 += 1
            time.sleep(0.1)
        else:
            raise SeleniumError('下拉框中没有数据！')
        for text in value:
            select_li = select_ul.exist('css selector', 'li', 'text:'.format(text), timeout=0.1)
            latest_uls = None
            old_uls = 1
            uls = select_ul.find_elements_by_attribute('tag name', 'li', timeout=self.timeout)
            while not select_li:
                if latest_uls == old_uls:
                    raise SeleniumError('无法找到对应的选项')
                old_uls = uls
                uls = select_ul.find_elements_by_attribute('tag name', 'li', timeout=self.timeout)
                uls[-1].find_element_by_attribute('css selector', 'span.h-checkbox', timeout=self.timeout).focus()
                select_li = select_ul.exist('css selector', 'li', 'text:' + text, timeout=0.1)
                latest_uls = uls
            select_li.find_element_by_attribute('css selector', 'span.h-checkbox', timeout=self.timeout).secure_click()

    def set_value_for_loading_single(self, value, timeout=20):
        """
        下拉继续加载的单选下拉框选择(消息系统，label为消息收发件人)

        :Args:
         - value: 选择项的文本集合
        """
        select_ul_class = r'h-select-dropdown-list'
        # 弹出下拉框
        self._element.find_element_by_attribute(
            'css selector', 'div.h-select-selection', timeout=self.timeout).secure_click()
        select_div = self._element.find_element_by_attribute(
            'css selector', 'div.h-select-dropdown', timeout=self.timeout)
        timeout1 = 0
        while timeout1 < timeout * 10:
            select_ul = select_div.exist('css selector', 'ul.' + select_ul_class, timeout=0.1)
            if select_ul:
                if select_ul.is_displayed():
                    break
            timeout1 += 1
            time.sleep(0.1)
        else:
            raise SeleniumError('下拉框中没有数据！')
        select_li = select_ul.exist('css selector', 'li', 'text:'.format(value), timeout=0.1)
        latest_uls = None
        old_uls = 1
        uls = select_ul.find_elements_by_attribute('tag name', 'li', timeout=self.timeout)
        while not select_li:
            if latest_uls == old_uls:
                raise SeleniumError('无法找到对应的选项')
            old_uls = uls
            uls = select_ul.find_elements_by_attribute('tag name', 'li', timeout=self.timeout)
            uls[-1].focus()
            select_li = select_ul.exist('css selector', 'li', 'text:' + value, timeout=self.timeout)
            latest_uls = uls
        select_li.secure_click()

    def set_value_for_pop_single_type(self, value, timeout=20):
        """
        处理下拉框数据以弹框形式展示的单选下拉框（例：label为证书类型的，等）

        :Args:
         - value: 选择项的文本
         - timeout: 超时时间，默认是20s

        :Example:
         - 例1: select.set_value_for_certificate_type('苹果')
         - 例2: select.set_value_for_certificate_type('苹果',timeout=5)
        """
        select_ul_class = r'h-select-dropdown-list'
        self._element.find_element_by_attribute(
            'css selector', 'div.h-select-selection', timeout=self.timeout).secure_click()     
        select_div = self._driver.find_element_by_attribute(
            'css selector', 'div.h-select-dropdown.h-select-dropdown-transfer.v-transfer-dom', timeout=self.timeout)
        timeout1 = 0
        while timeout1 < timeout * 10:
            select_ul = select_div.exist('css selector', 'ul.' + select_ul_class, timeout=0.1)
            if select_ul.is_displayed():
                break
            timeout1 += 1
            time.sleep(0.1)
        else:
            raise SeleniumError('下拉框中没有数据！')
        select_ul.find_element_by_attribute(
            'tag name', 'li', 'text:' + value, timeout=self.timeout).secure_click()

    def is_default_for_single(self, value):
        """
        检查操作员中心单选下拉框默认值
        重写说明：操作员中心下拉框默认值有在h-select-input弹框中的，需要兼容到

        :Args:
         - value: 待检查的选项值

        :Returns:
            匹配返回True，否则返回False

        :Example:
         - 例: select.is_default_for_single('香蕉')
        """
        span_value = self._element.find_element_by_attribute(
            'css selector', 'div.h-select-selection>span.h-select-selected-value', timeout=0.1)
        text = span_value.text
        if text:
            if value == text:
                return True
            else:
                logger.error('默认值不匹配！')
                return False
        span_default = self._element.exist(
            'css selector', 'div.h-select-selection>span.h-select-placeholder', timeout=0.1)
        if span_default and span_default.is_displayed():
            text = span_default.text
            if value == text:
                return True
            else:
                logger.error('默认值不匹配！')
                return False
        input_default = self._element.exist('css selector', 'input.h-select-input', timeout=self.timeout)
        if input_default:
            if input_default.text:
                text = input_default.text
            elif input_default.get_attribute('value'):
                text = input_default.get_attribute('value')
            elif input_default.get_attribute('placeholder'):
                text = input_default.get_attribute('placeholder')
            elif input_default.get_attribute('title'):
                text = input_default.get_attribute('title')
            else:
                text = ''
            if value == text:
                return True
            else:
                logger.error('默认值不匹配！')
                return False
        else:
            logger.error('没有默认值！')
            return False

    def is_default_for_multiple(self, value):
        """
        检查操作员中心多选下拉框默认值
        重写说明：操作员中心下拉框默认值有在h-select-input弹框中的，需要兼容到

        :Args:
         - value: 待检查的选项值

        :Returns:
            匹配返回True，否则返回False

        :Example:
         - 例: select.is_default_for_multiple('香蕉,苹果,葡萄')
        """
        span_value_list = self._element.find_elements_by_attribute(
            'css selector', 'div.h-select-selection>div.h-tag>span.h-tag-text', timeout=0.1)
        if span_value_list:
            for span_value in span_value_list:
                text = span_value.text
                for va in value:
                    if va == text:
                        break
                else:
                    logger.error('默认值不匹配！')
                    return False
            else:
                return True
        span_default = self._element.exist(
            'css selector', 'div.h-select-selection>span.h-select-placeholder', timeout=0.1)
        if span_default and span_default.is_displayed():
            text = span_default.text
            if value[0] == text:
                return True
            else:
                logger.error('默认值不匹配！')
                return False
        input_default = self._element.exist('css selector', 'input.h-select-input', timeout=self.timeout)
        if input_default:
            if input_default.text:
                text = input_default.text
            elif input_default.get_attribute('value'):
                text = input_default.get_attribute('value')
            elif input_default.get_attribute('placeholder'):
                text = input_default.get_attribute('placeholder')
            elif input_default.get_attribute('title'):
                text = input_default.get_attribute('title')
            else:
                text = ''
            if value[0] == text:
                return True
            else:
                logger.error('默认值不匹配！')
                return False
        else:
            logger.error('没有默认值！')
            return False